var fs = require("fs");
var path = require("path");

exports.getAllAlbums = function (callback) {
    fs.readdir("./uploads", function (err, files) {
        if(err){
            callback("没有找到uploads文件夹",null);
            return;
        }

        var allAlbums = [];

        //异步转同步 遍历路径下文件夹
        (function iterator(i) {
            if (i == files.length) {
                callback(null,allAlbums);
                return;
            }

            //console.log(path.join(__dirname,"../uploads/",files[i])); //使用__dirname 绝对路径
            //console.log("./uploads/" + files[i]); //相对路径

            fs.stat("./uploads/" + files[i], function (err, stats) {
                if(err){
                    callback("找不到文件"+files[i],null);
                    return;                                             //return 出错提前返回 终止函数调用
                }

                if (stats.isDirectory()) {
                    allAlbums.push(files[i]);
                }
                iterator(i + 1);
            });
        })(0);

    });
    return ["小猫", "小狗"];
};